package com.glory.dmegcpda.viewmodel

import androidx.lifecycle.MutableLiveData
import com.glory.dmegcpda.adapter.LevelAdapter
import com.glory.dmegcpda.bean.DefectBean
import com.glory.dmegcpda.bean.LotInfoDownLoadBean
import com.glory.dmegcpda.model.MaterialReceiveRepository
import com.glory.dmegcpda.model.QualityCheckRepository
import com.glory.mvvmnet.base.BaseViewModel
import com.glory.mvvmnet.entity.CallObserveBean
import com.glory.mvvmnet.entity.ToastMessageBean
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import java.io.Serializable

/**
 * FileName: QualityCheckViewModel
 * Author: XJH
 * Date: 2021/10/13 15:01
 * Description:
 * History:
 */
class QualityCheckViewModel : BaseViewModel() {

    val qualityCheckRepository by lazy { QualityCheckRepository() }
    var lotEntity = MutableLiveData<LotInfoDownLoadBean>()
    var defectTypeList: ArrayList<String> = arrayListOf()
    var defectReasonBaseList: ArrayList<DefectBean.AdrefBean> = arrayListOf()
    var defectTypeBaseList: ArrayList<DefectBean.AdrefBean> = arrayListOf()
    var defectReasonList: ArrayList<String> = arrayListOf()
    var mAdapter: LevelAdapter = LevelAdapter(arrayListOf())
    var holdCodeList: ArrayList<String> = arrayListOf()
    var releaseCodeList: ArrayList<String> = arrayListOf()

    fun getLotInfo(lot: String) {
        clearData()
        val map = JsonObject()
        map.addProperty("LOTID", lot)
        launchOnlyResult({
            val lotInfo = qualityCheckRepository.getLotInfo(map)
            lotEntity.value = lotInfo
            defUI.callObserve.value = CallObserveBean(101, 200)
        })
    }

    private fun clearData() {
        defectReasonList.clear()

        mAdapter.position = -1
        mAdapter.isSelector = false
        mAdapter.level = ""
        mAdapter.data.clear()
        mAdapter.notifyDataSetChanged()
    }

    fun init() {
        val defectTypeMap = JsonObject()
        defectTypeMap.addProperty("REFERENCENAME", "DefectCode")
        defectTypeMap.addProperty("LEVEL", 1)

        val defectReasonMap = JsonObject()
        defectReasonMap.addProperty("REFERENCENAME", "DefectCode")
        defectReasonMap.addProperty("LEVEL", 2)

        val holdCodeMap = JsonObject()
        holdCodeMap.addProperty("ACTIONTYPE", "GETBYREFNAME")
        holdCodeMap.addProperty("REFNAME", "HoldCode")

        val releaseMap = JsonObject()
        releaseMap.addProperty("ACTIONTYPE","GETBYREFNAME")
        releaseMap.addProperty("REFNAME","ReleaseCode")

        launchOnlyResult({
            val defect = qualityCheckRepository.getDefect(defectTypeMap)
            val defect1 = qualityCheckRepository.getDefect(defectReasonMap)
            if (defect.ADOWNERREFLISTLIST.isNotEmpty()) {
                defectTypeBaseList.addAll(defect.ADOWNERREFLISTLIST)
                defect.ADOWNERREFLISTLIST.forEach {
                    defectTypeList.add(it.KEYID + "##" + it.DESCRIPTION)
                }
            }

            if (defect1.ADOWNERREFLISTLIST.isNotEmpty()) {
                defectReasonBaseList.addAll(defect1.ADOWNERREFLISTLIST)
            }

            val holdCode = qualityCheckRepository.getHoldCode(holdCodeMap)
            if (holdCode.ADUREFLIST.isNotEmpty()) {
                holdCode.ADUREFLIST.forEach {
                    holdCodeList.add(it.KEYID + "##" + it.DESCRIPTION)
                }

            }

            val releaseCode = qualityCheckRepository.getReleaseCode(releaseMap)
            if (releaseCode.ADUREFLIST.isNotEmpty()){
                releaseCode.ADUREFLIST.forEach{
                    releaseCodeList.add(it.KEYID + "##" + it.DESCRIPTION)
                }
            }
        })
    }

    fun getLevel(){
        val levelMap = JsonObject()
        levelMap.addProperty("ACTIONTYPE", "GETBYREFNAME")
        levelMap.addProperty("REFNAME", "GradeCode")

        launchOnlyResult({
            val level = qualityCheckRepository.getLevel(levelMap)
            mAdapter.level = lotEntity.value!!.LOT.GRADE1
            if (level.ADREFLIST.isNotEmpty()){
                mAdapter.addData(level.ADREFLIST)
            }
        })
    }


    fun lotJudge(lot: String,type : String,reason : String,commit : String) {
        if (mAdapter.mLevel.isEmpty()) {
            defUI.toastEvent.value = ToastMessageBean("请选择等级", false)
            return
        }
        var requestList : ArrayList<DefectRequestBean> = arrayListOf()
        var entity = DefectRequestBean(type,reason)
        requestList.add(entity)
        val map = JsonObject()
        map.addProperty("LOTID", lot)
        map.addProperty("GRADE1", mAdapter.mLevel)
        map.addProperty("ACTIONCOMMENT", commit)
        map.add("DEFECTLIST", JsonParser().parse(Gson().toJson(requestList)).asJsonArray)
        launchOnlyResult({
            qualityCheckRepository.getLotJudge(map)
            defUI.toastEvent.value = ToastMessageBean("重新判定等级成功", true)
        })
    }

    fun hold(lot : String ,code : String,reason : String,actionType : String,commit: String){
        val map = JsonObject()
        map.addProperty("LOTID",lot)
        map.addProperty("ACTIONTYPE",actionType)
        map.addProperty("ACTIONCODE",code)
        map.addProperty("ACTIONREASON",reason)
        map.addProperty("ACTIONCOMMENT", commit)
        launchOnlyResult({
            qualityCheckRepository.holdLot(map)
            defUI.toastEvent.value = ToastMessageBean(if (actionType == "HOLD")"hold成功" else "放行成功", true)
        })

    }
    fun getDefectReason(type: String) {
        val typeRRN = getTypeRRN(type.split("##")[0])
        defectReasonBaseList.forEach {
            if (typeRRN == it.PARENTRRN) {
                defectReasonList.add(it.KEYID + "##" + it.DESCRIPTION)
            }
        }
    }

    private fun getTypeRRN(type: String): String {
        defectTypeBaseList.forEach {
            if (type == it.KEYID) {
                return it.OBJECTRRN
            }
        }
        return ""
    }

    data class DefectRequestBean(
        var DEFECTCODE : String = "",
        var DEFECTTYPE : String = ""
    ) : Serializable
}